<?php
/**
 * Created by PhpStorm.
 * User: junyv
 * Date: 2016/9/18
 * Time: 11:52
 */

namespace app\manage\controller;
class HouseGroup extends Base
{
    public function _initialize(){
        parent::_initialize();
    }
    //搜索
    public function _search(){
        $map      = [];
        ($keyword = input('get.keyword')) && $map['title'] = ['like', '%'.$keyword.'%'];
        $status   = -1;
        if(isset($_GET['status']) && $_GET['status'] !== ''){
            $status = input('get.status/d');
        }
        $status>=0 && $map['status'] = ['eq',$status];
        ($city = input('get.city/d',0)) && $map['city'] = $city;
        $this->assign('search', array(
            'status'  => $status,
            'keyword' => $keyword,
            'city' => $city
        ));
        return $map;
    }
    //添加图片
    public function addDo(){
        \app\common\model\HouseGroup::event('before_insert',function($obj){
            $images = $this->getPic();
            if($images){
                $obj->file = $images;
            }
            $house_info = $this->getHouseInfo($obj->house_id);
            $obj->begin_time = strtotime($obj->begin_time);
            $obj->end_time   = strtotime($obj->end_time);
            $obj->house_title = $house_info['title'];
            $obj->city        = $house_info['city'];
            $obj->area        = $house_info['area'];
            return true;
        });
        \app\common\model\HouseGroup::event('after_insert',function($obj){
            //更改楼盘状态
            model('house')->save(['is_group'=>1,'group_youhui'=>$obj->youhui],['id'=>$obj->house_id]);
        });
        parent::addDo();
    }
    //编辑团购
    public function editDo(){
        \app\common\model\HouseGroup::event('before_update',function($obj){
            $images = $this->getPic();
            if($images){
                $obj->file = $images;
            }
            $house_info = $this->getHouseInfo($obj->house_id);
            $obj->begin_time = strtotime($obj->begin_time);
            $obj->end_time   = strtotime($obj->end_time);
            $obj->house_title = $house_info['title'];
            $obj->city        = $house_info['city'];
            $obj->area        = $house_info['area'];
            return true;
        });
        parent::editDo();
    }

    //删除团购
    public function delete(){
        \app\common\model\HouseGroup::event('after_delete',function($obj){
            if($obj->img){
                @unlink('.'.$obj->img);
            }
            if($obj->file){
                foreach($obj->file as $v){
                    @unlink('.'.$v['url']);
                }
            }
            //更改楼盘状态
            model('house')->save(['is_group'=>0,'group_youhui'=>''],['id'=>$obj->house_id]);
        });
        parent::delete();
    }
    public function ajaxCheckTitle(){
        $title = input('get.J_title');
        $id    = input('param.id/d',0);
        $return['status'] = 1;
        $where['title'] = $title;
        $id && $where['id'] = ['neq',$id];
        $count = model('house_group')->where($where)->count();
        if($count > 0){
            $return['status'] = 0;
        }
        return json($return);
    }
    //获取楼盘名称
    private function getHouseInfo($id){
        $info = model('house')->where(['id'=>$id])->field('city,area,title')->find();
        return $info;
    }
    /**
     * @param $obj
     * 添加图片
     */
    private function getPic(){
        $insert = [];
        if(isset($_POST['pic']) && !empty($_POST['pic'])) {
            $images = $_POST['pic'];
            foreach ($images['pic'] as $key => $v) {
                $insert[] = [
                    'url' => $v,
                    'title' => $images['alt'][$key],
                ];
            }
        }
        return $insert;
    }

}